<template>
	<view class="user">
		<!-- 上方显示用户信息的区域 -->
		<view class="userinfo">
			<!-- 信息区域 -->
			<view class="top">
				<view class="avator">
					<uni-icons type="person-filled" size="30" :color="logColor"></uni-icons>
				</view>
				<view class="uname" v-if="store.isLogin">
					<view>{{store.userInfo.username}}</view>
					<view>{{store.userInfo.mobile}}</view>
				</view>
				<view class="uname" v-else @tap="toLogin">点击登录</view>
			</view>
		</view>
		<!-- 下方显示的四项用户操作项 -->
		<view class="list">
			<uni-list>
				<uni-list-item v-for="item,index in manageList" :title="item.name" :show-extra-icon="true"
					:extra-icon="item.icon" clickable link @tap="handleItemClick(index)"></uni-list-item>
			</uni-list>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import { userStore } from '../../store/userStore'


	// 创建状态库
	let store = userStore()
	let mystore = userStore()
	let logColor = ref("#ccc")
	const manageList = ref([{
		name: '订单管理',
		icon: {
			color: '#56c48b',
			size: '22',
			type: 'shop'
		}
	}, {
		name: '地址管理',
		icon: {
			color: '#ff6700',
			size: '22',
			type: 'location-filled'
		}
	}, {
		name: '我的收藏',
		icon: {
			color: '#0225ff',
			size: '22',
			type: 'star'
		}
	}, {
		name: '退出登录',
		icon: {
			color: '#0081b9',
			size: '22',
			type: 'gear'
		}
	}])

	function handleItemClick(index) {
		console.log('Clicked index:', index);
		switch (index) {
			case 0:
				console.log('美食景点收藏被点击');
				break;
			case 1:
				console.log('电影收藏被点击');
				break;
			case 2:
				console.log('修改密码被点击');
				break;
			case 3:
				handleLogout(); // 调用退出登录函数
				break;
			default:
				console.log('未处理的索引');
		}
	}

	function handleLogout() {
		uni.showModal({
			content: '确定要退出登录吗？',
			success: (res) => {
				if (res.confirm) {
					// 清除用户信息
					store.clearUserInfo();
					// 跳转到登录页面
					uni.navigateBack()
				}
			}
		});
	}

	function navto(index) {

		uni.showToast({
			title: `你点击的是${manageList.value[index].name}`
		})
	}

	function toLogin() {
		uni.navigateTo({
			url: "/pages/login/login"
		})
	}
</script>

<style lang="scss">
	/* 用户信息区域样式 */
	/* 基础样式 */
	body {
		font-family: 'Arial', sans-serif;
		background-color: #f4f4f4;
		margin: 0;
		padding: 0;
	}

	/* 用户信息区域样式 */
	.userinfo {
		background-color: #ffffff;
		padding: 20px;
		text-align: center;
		box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
	}

	.top {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.avator {
		border-radius: 50%;
		border: 1px solid #ddd;
		padding: 5px;
		display: inline-block;
	}

	.avator uni-icons {
		margin: 0;
		padding: 0;
	}

	.uname {
		display: flex;
		flex-direction: column;
		margin-left: 15px;
	}

	.uname view {
		margin: 5px 0;
	}

	/* 登录按钮样式 */
	.uname view:not(:first-child) {
		color: #888;
		font-size: 14px;
	}

	/* 用户操作项样式 */
	.list {
		margin-top: 20px;
	}

	uni-list {
		background-color: #ffffff;
		padding: 0;
		margin: 0;
	}

	uni-list-item {
		padding: 15px;
		border-bottom: 1px solid #eee;
	}

	uni-list-item:last-child {
		border-bottom: none;
	}

	uni-list-item:active {
		background-color: #f0f0f0;
	}

	/* 点击登录样式 */
	.uname[view="v-else"] {
		cursor: pointer;
		color: #007aff;
	}

	.uname[view="v-else"]:hover {
		text-decoration: underline;
	}
</style>